package com.cstar.learnlang.PageControllers;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.cstar.learnlang.DataForms.FeedbackDataForm;
import com.cstar.learnlang.DataForms.LoginDataForm;

@Controller
@RequestMapping(value={"/otherPages/feedbackPost", "/otherPages/feedbackPage"})
@SessionAttributes
public class FeedbackPageController {
	private static final Logger logger = LoggerFactory.getLogger(FeedbackPageController.class);
	@Autowired
	private HomePageController homePageController;
	@Autowired
	private JdbcTemplate jdbcRegisterInfoTemplate;
	@RequestMapping(value = "/otherPages/feedbackPage", method = RequestMethod.GET)
	public String feedbackPage(HttpServletRequest request,
			HttpServletResponse response,
    		HttpSession session,
			@RequestParam(value="logout", required=false) String logout, 
			@RequestParam(value="ln", required=false) String ln) {
		logger.info("feedbackPage");
		homePageController.ProcessLanguageSwitchAndLogout(request, response, session, ln, logout);
		session.setAttribute("pageName", "feedback");
		return "otherPages/feedbackPage";
	}
	@RequestMapping(value="/otherPages/feedbackPost", method = RequestMethod.POST)
    public ModelAndView feedbackPost(HttpServletRequest request, 
    		@ModelAttribute("feedbackInfo") FeedbackDataForm feedbackInfo, 
    		BindingResult result) {
        System.out.println("email:" + feedbackInfo.getComments());
        String sql;
		ModelAndView mv;
		mv = new ModelAndView ("redirect:/otherPages/feedbackPage");

        sql = "SELECT COUNT(*) FROM FEEDBACK WHERE USERNAME = ? AND IP = ?";
		int commentCount = jdbcRegisterInfoTemplate.queryForInt(sql, new Object[] {
				feedbackInfo.getUsername(),
				request.getRemoteAddr()
			});
		if (commentCount > 5) {
			mv.getModelMap().addAttribute("tooManyComments", "1");
		} else {
			sql = "INSERT INTO FEEDBACK (COMMENTS, USERNAME, DATETIME, IP) VALUES (?, ?, ?, ?)";
			DateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");
			Date date = new Date ();
			jdbcRegisterInfoTemplate.update(sql, new Object[] {
					feedbackInfo.getComments(),
					feedbackInfo.getUsername(),
					dateFormat.format (date),
					request.getRemoteAddr()
				});
			mv.getModelMap().addAttribute("feedbackSent", "1");
		} // if
        return mv;
    }
	@ModelAttribute("feedbackInfo")
    public FeedbackDataForm getFeedbackInfoObj() {
        return new FeedbackDataForm();
    }
}
